package com.shengruan.index.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.vo.User;
import com.shengruan.common.Constants;
import com.shengruan.common.OnLineUser;
import com.shengruan.framework.action.ActionSupport;

public class LogoutAction extends ActionSupport {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void execute() throws Exception {
        HttpSession session = request.getSession();


        /***从在线用户中移除***/

        User currentUser = (User) session.getAttribute(Constants.USER);
        OnLineUser.removeUser(currentUser);

        session.removeAttribute(Constants.USER);
        session.removeAttribute(Constants.USER_MESSAGE_COUNT);
        session.invalidate();

        Cookie[] cookies = request.getCookies();
        if (cookies!=null) {
            for (Cookie cookie : cookies) {
                if (Constants.COOKIE_REMEMBERME_KEY.equals(cookie.getName())) {
                    cookie.setValue("");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }

        this.redirect("/index.ac");
    }
}
